import {
  findIndex,
  get,
  has,
  isEmpty,
  reverse
} from 'lodash'

export default {
  data () {
    return {
      instances: [],
      nodeData: []
    }
  },
  methods: {
    handleNodeClick (data, treeNode, instance) {

    },

    setCurrentNodeStyle () {
      this.instances.forEach(instance => {
        instance.node.isCurrent = true
        instance.node.checked = true
      })
    },
    getCurrentInstance (instance) {
      const findInstanceIndex = findIndex(this.instances, ['_uid', instance._uid])
      if (findInstanceIndex !== -1) {
        this.instances.splice(findInstanceIndex, 1, instance)
      } else {
        this.instances.push(instance)
      }
    },
    getCurrentNodeData () {
      const nodeData = []
      const getData = (node, level = 0) => {
        let data = get(node, 'data')
        if (level === 0 && has(data, 'children')) {
          delete data.children
        }
        nodeData.push(data)
        if (!isEmpty(has(node, 'parent'))) {
          level++
          this.getData(get(node, 'parent'), level)
        }
      }
      getData()
      this.nodeData = reverse(nodeData)
    }
  }
}
